//
//  ZYProblemRecoverTwoVC.m
//  YiLin
//
//  Created by Zyark on 2018/5/30.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYProblemRecoverTwoVC.h"
#import "ZYProblemRecoverThreeVC.h"
#import "ZYRecoverPasswordViewModel.h"

@interface ZYProblemRecoverTwoVC ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong) UIPickerView *problemPickerView;
@property(nonatomic,strong) UITextField *problemTextField;
@property(nonatomic,strong) UITextField *answerTextField;
@property(nonatomic,strong) UIButton *confirmBtn;
@property(nonatomic,strong) NSArray *problemArray;
/**
 校验问题答案
 */
@property(nonatomic,strong) ZYRecoverPasswordViewModel *checkProblemViewModel;

@end

@implementation ZYProblemRecoverTwoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self setupTap];
    [self bindView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_PROBLEM_RECOVER_CONTROLLER_TITLE];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(void)setupView{
    CGFloat left = 20;
    CGFloat height = 40;
    CGFloat padding = 26;
    UIPickerView *problemPickerView = [[UIPickerView alloc] init];
//    problemPickerView.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    problemPickerView.backgroundColor = [UIColor whiteColor];
    problemPickerView.delegate = self;
    problemPickerView.dataSource = self;
//    problemPickerView.hidden = YES;
//    [self.view addSubview:problemPickerView];
    
    UITextField *problemTextField = [[UITextField alloc] init];
    problemTextField.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    [self.view addSubview:problemTextField];
    problemTextField.centerX = self.view.centerX;
    problemTextField.top = self.navigationView.bottom + 20;
    [self setupTextField:problemTextField];
//    problemTextField.delegate = self;
    problemTextField.inputView = problemPickerView;
    problemTextField.text = self.problemArray[0];
    
    UITextField *answerTextField = [[UITextField alloc] init];
    answerTextField.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    [self.view addSubview:answerTextField];
    answerTextField.centerX = self.view.centerX;
    answerTextField.top = problemTextField.bottom + padding;
    [self setupTextField:answerTextField];
    answerTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.bounds = answerTextField.bounds;
    [self.view addSubview:confirmBtn];
    confirmBtn.centerX = self.view.centerX;
    confirmBtn.top = answerTextField.bottom + 65;
    [ZYUtils setGradualChangingColorWithButton:confirmBtn size:confirmBtn.size];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    confirmBtn.layer.cornerRadius = 5;
    confirmBtn.layer.masksToBounds = YES;
    [confirmBtn addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    
    _confirmBtn = confirmBtn;
    _answerTextField = answerTextField;
    _problemTextField = problemTextField;
    _problemPickerView = problemPickerView;
    [self setupTitle];
//    [_problemPickerView reloadAllComponents];
}

-(void)bindView{
    @weakify(self);
    self.checkProblemViewModel.username = _username;
    [self.checkProblemViewModel.problemListCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        self.problemArray = @[x];
        self.problemTextField.text = x;
        [self.problemPickerView selectRow:0 inComponent:0 animated:YES];
        [self.problemPickerView reloadAllComponents];
    }];
    [self.checkProblemViewModel.problemListCommand execute:nil];
}

-(void)setupTextField:(UITextField *)textFiled{
    textFiled.layer.borderWidth = 1;
    textFiled.layer.borderColor = RGBA(235, 235, 235, 1).CGColor;
    textFiled.layer.cornerRadius = 5;
    textFiled.layer.masksToBounds = YES;
    [self setLeftView:textFiled];
}

/**
 初始化标题
 */
-(void)setupTitle{
    NSString *answerPlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_PROBLEM_RECOVER_ANSWER_PLACEHOLDER];
    NSString *confirmBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE];
    [_confirmBtn setTitle:confirmBtnTitle forState:UIControlStateNormal];
    _answerTextField.placeholder = answerPlaceholder;
}

/**
 设置左视图
 @param textField texField
 */
-(void)setLeftView:(UITextField *)textField{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, textField.height)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}

#pragma mark -Event
-(void)doConfirm:(id)button{
    [self dismissKeyboard];
    if(!_problemArray || [_problemArray count] ==0){
        [[ToastHelper sharedToastHelper] toast:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ERROR_PROBLEM_NOT_SETTING]];
        return;
    }
    self.checkProblemViewModel.phone = _username;
    self.checkProblemViewModel.problemAnswer = _answerTextField.text;
    @weakify(self);
    [[self.checkProblemViewModel.problemCheckCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        ZYProblemRecoverThreeVC *vc = [[ZYProblemRecoverThreeVC alloc] init];
        vc.phone = _username;
        vc.problemId = [_problemArray lastObject];
        vc.answer = _answerTextField.text;
        [self.navigationController pushViewController:vc animated:YES];
    } error:^(NSError *error) {
        [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
    }];
    //监听状态
    [[self.checkProblemViewModel.problemCheckCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        ((UIButton *)button).enabled = !executing;
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
    
}

//#pragma mark - textField delegate
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//    return NO;
//}

#pragma mark UIPickerViewDelegate DataSource
//返回有几列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//返回指定列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.problemArray count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.problemArray[row];
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [_problemTextField resignFirstResponder];
    _problemTextField.text = self.problemArray[row];
}

#pragma mark -Getter
-(ZYRecoverPasswordViewModel *)checkProblemViewModel{
    if(!_checkProblemViewModel){
        _checkProblemViewModel = [[ZYRecoverPasswordViewModel alloc] init];
    }
    return _checkProblemViewModel;
}
@end
